import java.io.*;

public class HttpOutputStream extends BufferedOutputStream {
	private boolean sendHeader = false;
	private boolean sendBody = false;
	private String header = "";
	private String ver = "";
	private int code = 0;
	
	//HTTP header, body, version
	public HttpOutputStream(OutputStream out, String ver, String method) {
		super(out);
		sendHeader = !(ver.equals(HTTP.HTTP_0_9));
		sendBody = !(method.equals(HTTP.METHOD_HEAD));
		this.ver = ver;
	}
	
	public void setCode(int code) {
		this.code = code;
	}
	
	public void setHeader(String field, String value) {
		header = header +field+ ": " +value+ (char)HTTP.CR+(char)HTTP.LF;
	}
	
	public void sendHeader() throws IOException {
		String sLine = ver+ " " +code+ " "+
			HTTPconf.getCodeMessage(code)+
			(char)HTTP.CR+(char)HTTP.LF;
		
		if(sendHeader) {
			if(!header.equals(""))
				sLine += header;
			sLine = sLine + (char)HTTP.CR+(char)HTTP.LF;
			super.write(sLine.getBytes());
		}
		super.flush();
	}
	
	public void write(String msg) throws IOException {
		if(sendBody)
			super.write(msg.getBytes());
		super.flush();
	}
	public void write(byte [] msg) throws IOException {
		if(sendBody)
			super.write(msg);
		super.flush();
	}
	public void write(byte [] msg, int off, int len) throws IOException {
		if(sendBody)
			super.write(msg, off, len);
		super.flush();
	}
}
